<?php
namespace Common\Model;

use Think\Model;

class OrdersModel extends Model
{
    protected $_validate = [
        ['img_url1', 'require', '请上传图片']

    ];

    //新增订单
    public function orderAdd()
    {
        if (!$this->create()) {
            $arr['state'] = 'error';
            $arr['info'] = $this->getError();
            return $arr;
        } else {
            if ($rid=$this->add($_POST)) {
                $arr['rid']=$rid;
                $arr['state'] = 'success';
                $arr['info'] = '提交订单成功';
            } else {
                $arr['state'] = 'error';
                $arr['info'] = '提交失败';
            }
            return $arr;
        }
    }

    //订单查询
    public function myOrderList($user_id)
    {

        $counts = $this->where("user_id=%d and status>=%d", [$user_id, 0])->count();
        $page = page($counts, 10, 5);
        $list = $this->where("user_id=%d and status>=%d", [$user_id,0])->order("rid desc")->limit($page->firstRow, $page->listRows)->select();
        $arr['list'] = $list;
        $arr['page'] = $page;
        return $arr;


    }

    //后台订单列表
    public function orderList($status)
    {
        $area_id = $_SESSION['tc']['area_id'];
        $counts = $this->where("status=%d and belong_to_area=%d", [$status, $area_id])->count();
        $page = page($counts, 20, 5);
        $list = $this->where("status=%d and belong_to_area=%d", [$status, $area_id])
            ->order("rid DESC")
            ->limit($page->firstRow, $page->listRows)->select();
        $arr['page'] = $page;
        $arr['list'] = $list;
        return $arr;
    }
    //单个订单详情查询
    public function detail($rid)
    {
       return $this->where("rid=%d",$rid)->find();
    }

    //判断订单状态
    public function orderStatus($rid)
    {

        if(empty($rid)){
            return 101;
        }else{
            $re=$this->detail($rid);
            if($re['status'] ==0){
                return 200;
            }else{
                return 102;
            }
        }
    }
}
















